Okay, so last lesson I introduced you to tables and we looked at how to create a table, add values
into your table, delete add values from your table.
But I kind of said that you know they're really useful.
But I never really got really looked at any examples as to why, which is what we're looking at today.
Okay.
So add a script into your game.
We're going to talk about tables okay.
So first off um let's get let's make another table.
So let's say local I'm going to give this one a I'm going to make a proper table.
This time we're going to make a table with some foods in them okay.
Local foods is equal to.
And let's just write down some food.
Foods.
Apple.
Burger.
Um orange.
What.
Comes out each one pineapple.
And let's say um pizza for example.
Okay.
Just just a, just a basic example for, for this case okay.
So we know how to reference one of them.
We know how to add one in how to take one away.
Um, but we don't really can do much else with these.
Um, for example, if we wanted to check the, um, one of them if we wanted to.
Yeah.
Let's say that we have a table and we don't actually know what each place they have.
Okay.
So for example, your banned players list, if you want to remove a player from that list, you'd have
to first find out where they are on the list, okay.
Because you're not going to have made one manually by yourself here because it would be like, you know,
thousands of lines long.
Um, so you're not really sure where the player is in a list, um, or other cases, maybe you want
to check, maybe you've referenced objects in your list and you want to check.
You know, which ones are anchored.
You can't go for each one and say um, is foods one equal to this?
Yes.
No.
You know, you can't do that.
It would take too long.
Um, where are we going to find out a shorter way to do it.
Okay.
Now you already know about for loops.
Okay.
Um, for o for I is equal to one comma, ten comma one do for example.
Okay, well, we can use a modified version of a for loop for this okay.
So when I said how there's like um three loops right at the start I technically lied there's kind of
four.
Um, but it's just a modified version I think of it.
Okay.
Um, now I'm going to write out first, and then I'm going to explain what it does, because I think
it would help if you can see it all at once.
So you'd write it.
As for I comma v in pairs for foods do.
Okay.
You'd write it out like this.
Here.
Now, um, hopefully you remember what I means.
Okay, I is just a number which goes up by one each time.
So every time this for loop runs, I will increase by one in this case.
So if we just print I and run the game we can just see that we go 1234, five and it will actually stop
at five in this case okay.
But it just kind of ticks up 12345 okay.
So the I variable here, it just stores a number ticking up by one V stands for value I guess.
And what v is is the actual value in the table.
So remember how last lesson I printed out a table and it said one is equal to this.
Two is equal to this, three is equal to this.
Well that's kind of how this works.
So maybe if I print the foods table again.
And let's get rid of printing I we don't need to do that again.
Just to show you a bit more about this, um, we have here, one is equal to Apple, two is equal to
burger, three orange, four pineapple, five pizza.
Now when we printed I, I went from 1 to 5.
So actually the one two, three, four five bit is actually the I values.
They go up from 1 to 5.
Okay.
And the v values are on the right hand side here.
Apple burger orange pineapple and pizza.
Veda values the term we are getting from our table.
Okay.
So basically when I is equal to one, v is equal to apple, when I is equal to two, v is equal to burger.
So it's a way of going through your table and checking if each value is a certain thing, or just going
through the values in your table.
Okay.
So that's what this I comma.
So I comma v bit means in pairs basically means that when I changes so does v they change in pairs.
Okay.
And in brackets here we've written down foods.
That's just giving it the name of our table.
Because obviously if it wants to go through our table it needs it needs the table to go through.
Okay.
Then it's just the rest of the then it's just like a for loop like normal.
You would write do at the end of a line and end once you don't no longer want the code included.
Okay.
So people do find this for I comma a loop a bit difficult, but it's just it's just cycling through
these different values in the table.
Really.
Okay.
Um, so let's see what we can actually do with that.
Well, let's say we wanted to, um, I don't know, we wanted to, um, delete, let's say, um, yes.
Let's say we only wanted to keep the first three values.
So we wanted to delete the last two values in our table.
Okay.
Well we can use AI for that because I will tick up from 1 to 5.
So if we want to delete the last two, we only want to keep the values when I is free or less than free.
So we can use an if statement for that.
We can say if AI is larger.
So if um yeah.
So if I is smaller than free, so free is greater than I or we, we should really be saying if free
is less than or equal to AI.
So if I is equal to one, 2 or 3, um, so other way around, actually wanting to delete the last two,
um, there's no script, there's no, there's no code to keep a value.
There's only code to delete it.
So we want to instead say if AI is greater than three so 4 or 5.
In this case then we can just say um table dot remove.
We know we're fruits here and renumber is just AI.
Um, because if AI is four we want to delete the four value and so on.
Okay.
So you can use the AI value for things like this.
It's quite cool.
Okay.
Um, so hopefully that gives you a basic understanding about how you can use this to cycle through your
tables.
Okay.
Um, you can use the AI values.
And also you can use the values to check certain things or anything like that.
Okay.
If these are objects that you've referenced, you could always say, um, you know, um, if v dot um,
yeah.
You can use the v value as well, not just the I.
So you could, you could print V, you could say if visa if these are objects you've reference, you
could say V dot transparency and so on okay.
So definitely have a play around with with that one there.
Now I'd actually probably recommend you to pause the um, lecture here for a minute.
Maybe just try that, especially if you're not too sure about that just yet, because we're going to
kind of bring some more things in to make it a bit more advanced.
Okay, so what if we want to create our own table, but not by writing it down?
Okay.
Um, we want to create a table of objects, okay, that are in the workspace or anywhere else in our
game.
Okay.
We can use the Get Children function to create a table full of an objects children.
So if I was to say game dot workspace colon get children.
That creates a table with all of the workspaces children in it.
So let's actually print this um, game dot workspace and see what happens when we print this.
It prints one baseplate, two terrain, three spawn location, and four camera.
There are four objects we have inside of the workspace.
Okay.
Let's say, um, let's add a few more parts into the workspace.
Let's just do a couple here.
Cool.
And let's run the game again.
It then also gets all of our parts here as well.
Okay.
So it's cycling through all of the children of the workspace.
Um, now this is where we can apply this to our for icon of um to basically say for I comma v in pairs.
Let's maybe add a folder inside here and say game dot workspace dot folder colon get children.
Just like that to there.
Because remember this creates a table full of all of the children of the of the parent in this case
workspace.
But in this case it's folder.
So basically this is going to create a table full of all of the folders children.
And then you can run some code which will actually happen for each of the folders children.
Or you could, you know, make sure the children have certain values or things like that.
Okay.
But the main the main thing I think this is used for is the first point I said about how it can be used
to run code for each, um, for each child in the folder.
Okay.
Um, so all of these parts we have in our game here.
Okay, let's say, um, we wanted to we wanted to, um, delete or delete all the parts in the workspace.
Okay.
Or delete all the parts in the folder.
This could be used, for example, if you have a building game where let's say players have five minutes
to build whatever they'd like, players judge on who's who's whose building is the best, but after
the time, it needs to delete everything.
So, um, you know, players can start over now.
You don't know how many parts the player is going to be used, going to have used in their, in their,
in their game.
So you can't really reference them correctly because you'd also have to give them all individual names,
which would be very difficult.
So instead you can just go through each of these and delete all everything, um, everything in that
folder.
So we could say V, which would basically be referencing one of one of the, one of the children in
the folder colon destroy.
So you can do whatever code that you already know with the I and V values.
Okay.
Let's add all these parts inside of the folder and let's make it a bit more interesting.
Let's also, you know say v dot color is equal to color three dot new.
Um, one comma, zero comma, zero.
And let's maybe, you know, also say um, wait, uh, half a second just so we can see it happening
in real time.
Okay, let's run the game and we can see these parts here getting deleted just like that.
So pretty cool.
Okay.
Um, so your for I comma v in pairs.
You've just been introduced to it now, but it's, it will be very useful for you in a lot of different
areas of coding okay.
Mostly to save you time.
Okay.
So let's say for example you're making a shop using GIS.
Okay.
And there are 30 different things that you can purchase from the shop.
So when, when when you click one of those 30 buttons you can buy that item.
Okay.
When people code it, do you think that they individually code the button for each 30 for one of each
30, um, items in the shop?
They don't.
They just create one bit of code which can be reused using a for loop for each one.
Okay, so when we have a look at making some of our own games and things like that, you will look about
some proper uses of this.
But it's a very, very powerful loop, which sure, you don't need it, but it will save you incredible
amounts of time.
And sometimes, to be honest, it's extremely difficult to do without it, especially when there's unknowns.
Like for example, it's an unknown amount of players or an unknown amounts of parts in your game.
Um, and it makes it makes coding much quicker to do.
Okay.
So hopefully I've kind of shown you, you know how powerful this is.
Um, try some examples out with it.
You know, maybe.
Try about try or guys try out with tools, you know, try out all these different things.
But at some point later on in the course you will get some other examples on this and especially showing
you how powerful it is.
Okay.
So that's it for tables.
Hopefully that's given you a good overview of them.
And join me in the next lesson.
We're going to do a bit of a little bit of maths.
Okay.
Um but that will be it for the this scripting section.
Okay.
So from the next lesson we're going to have a look at some maths that we can add into our scripts.